package com.easy;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created by Lxk on 2019/6/8.
 */
public class Solution387 {

    public int firstUniqChar(String s) {
        if(s==null){
            return -1;
        }
        Map<Character,Integer> map = new HashMap<>();
        for(int i = 0;i < s.length(); i++){
            if(map.containsKey(s.charAt(i))){
                map.put(s.charAt(i),map.get(s.charAt(i))+1);
            }else{
                map.put(s.charAt(i),1);
            }
        }
        Set<Character> set = new HashSet<>();
        for(Map.Entry<Character,Integer> entry : map.entrySet()){
            if(entry.getValue()==1){
                set.add(entry.getKey());
            }
        }
        for(int i = 0;i < s.length(); i++){
            if(set.contains(s.charAt(i))){
                return i;
            }
        }
        return -1;
    }



}
